public class OutstandingReviews{
    
    private Boolean selectAll = false;
    // the next 4 properties are used to display a message to the user upon action completion
    public String msgSummary {get;set;}
    public String msgDetail {get;set;}
    public Integer msgStrength {
        get{
            if (msgStrength == null) msgStrength = 1;
            return msgStrength;
        }
        set;    
    }
    public String msgSeverity {
        get{
            if (msgSeverity == null) msgSeverity = 'info';
            return msgSeverity; 
        }   
        set;
    }   
    public Position__c position {
        get{
            if(position == null) {
                position = [select Id,Name,Status__c,Sub_Status__c from Position__c where id = :System.currentPageReference().getParameters().get('id')];              
            }
            return position;
        }   
        set;
    }
   
    private List<Interviewer__c> interviewers {
        get{
            if(interviewers== null) interviewers= new List<Interviewer__c>();
            return interviewers;    
        }   
        set;
    }
    
    private List<Review__c> reviews {
        get{
            if(reviews == null) {
                reviews = [select r.id,r.name,r.review_completed__c,r.position_name__c,r.interviewer__c,r.interviewer_name__c,r.interviewer__r.Employee__r.Name,r.interviewer__r.role__c,r.interviewer__r.employee__c,r.interviewer__r.employee__r.email,r.job_application__c,r.job_application__r.name,r.candidate_name__c from review__c r where job_application__r.position__c = :position.Id and review_completed__c != true];
            }
            return reviews;
        }
        set;    
    }
    
    private List<Job_Application__c> jobApplications {
        get {
            if(jobApplications == null) jobApplications = new List<Job_Application__c>();
            return jobApplications; 
        }   
        set;
    }
    
    public List<MyOutstandingReview> myOutstandingReviews{
        get{
            // perform the logic to find the Reviews that have not yet been completed for the current Position
            if (myOutstandingReviews == null) {
                myOutstandingReviews = new List<MyOutstandingReview>();
                for(Review__c review:reviews){
                    MyOutstandingReview record = new MyOutstandingReview(review);
                    myOutstandingReviews.add(record);
                }
            }
            return myOutstandingReviews;
        }
        set;    
    }
    
    public PageReference toggleCheckbox(){
        return null;
    }
    
    public PageReference createTasks(){
        List<Task> tasks = new List<Task>();
        for(MyOutstandingReview myORev:myOutstandingReviews){
            if (myORev.checked) {
                Task t = new Task();
                t.WhatId = myORev.review.Id;
                t.OwnerId = myORev.review.interviewer__r.Employee__c;
                t.Subject = 'Interview Reminder';
                t.ActivityDate = System.today() + 3;
                t.IsReminderSet = true;
                t.ReminderDateTime = System.now()+1;
                t.Status = 'Not Started';
                t.Priority = 'Normal';
                t.Description = 'Please complete this review as soon as possible';
                tasks.add(t);
            }
        }
        // insert the tasks and update the pageMessage
        if (tasks.size() > 0) { 
            try{
                insert tasks; 
                // set the message for the user
                msgStrength = 1;
                msgSeverity = 'info';
                msgSummary = tasks.size() + ' tasks created successfully';
            } catch (DMLException e){
                msgStrength = 2;
                msgSeverity = 'error';
                msgSummary = 'An error occurred while attempting to create tasks';
                for (Integer i = 0; i < e.getNumDml(); i++) {
                    // Process exception here
                    System.debug(e.getDmlMessage(i));
                    msgDetail += e.getDmlMessage(i) + ' ; ';
                }
            }
        } else {
            // set the message for the user
            msgStrength = 1;
            msgSeverity = 'info';
            msgSummary = 'No tasks were created';
        }

        return null;
    }
    
    public PageReference sendEmails(){ 
        // this function sends emails to the interviewers selected by the user on the page
        
        // first we declare an empty List of emails
        Messaging.SingleEmailMessage[] emails = new Messaging.SingleEmailMessage[0];
 
        // loop through the Outstanding Reviews and create emails
        for(MyOutstandingReview myORev:myOutstandingReviews){
            if (myORev.checked) {
                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                email.setSaveAsActivity(false);
                email.setTargetObjectId(myORev.review.interviewer__r.employee__c);               
                email.setSubject('Reminder: Review ' + myORev.review.name + ' needs your attention');
                String htmlBody= 'Dear ' + myORev.review.interviewer_name__c + ':<br/><br/>';
                htmlBody += 'This is a reminder that your review of ' + myORev.review.Candidate_Name__c + ' for the position of ' + 
                    myORev.review.Position_Name__c + ' has not been completed yet. Please complete this review as soon as possible.' +
                    '<br/><br/>Review Link: <a href=https://prerelna1.pre.salesforce.com/' + myORev.review.id + '>click here</a><br/><br/>Thank You,<br/>Recruiting Dept.';
                email.setHtmlBody(htmlBody);
                String[] emailAddresses = new String[] { myORev.review.interviewer__r.employee__r.email };
                email.setToAddresses(emailAddresses);               
                emails.add(email);               
            }
        }

        if (emails.size() > 0) {
            try{
                Messaging.sendEmail(emails, true);
                // set the message for the user
                msgStrength = 1;
                msgSeverity = 'info';
                msgSummary = emails.size() + ' emails sent';
            }
            catch (Exception e){
                System.debug(e.getMessage());
                msgStrength = 2;
                msgSeverity = 'error';
                msgSummary = 'An error occurred while attempting to send email';
                msgDetail = e.getMessage();
            }
        }
        
        return null;
    }
    
    public PageReference doSelectAll(){
        selectAll = !selectAll;
        for(MyOutstandingReview myORev:myOutstandingReviews){
            myORev.checked = selectAll;
        }
        return null;
    }
}